package com.vincce.sport.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vincce.sport.core.utils.SimpleStorageClient;
import com.vincce.sport.entity.Advert;
import com.vincce.sport.entity.CompetitionSeason;
import com.vincce.sport.entity.Page;
import com.vincce.sport.entity.Player;
import com.vincce.sport.entity.PreliminaryContest;
import com.vincce.sport.entity.ShootSpot;
import com.vincce.sport.entity.Team;
import com.vincce.sport.entity.TeamDate;
import com.vincce.sport.mapper.AdvertMapper;
import com.vincce.sport.mapper.CompetitionSeasonMapper;
import com.vincce.sport.mapper.PlayerMapper;
import com.vincce.sport.mapper.PreliminaryContestMapper;
import com.vincce.sport.mapper.ShootSpotMapper;
import com.vincce.sport.mapper.TeamDateMapper;
import com.vincce.sport.mapper.TeamMapper;

@Controller
@RequestMapping("/dataStatic")
public class DataStaticsController {

	
	@Resource
	private PreliminaryContestMapper preliminaryContestMapper;
	@Resource
	private TeamMapper teamMapper;
	@Resource
	private TeamDateMapper teamDateMapper;
	@Resource
	private PlayerMapper playerMapper;
	@Resource
	private AdvertMapper advertMapper;
	@Resource
	private CompetitionSeasonMapper competitionSeasonMapper;
	@Resource
	private ShootSpotMapper shootSpotMapper;
	
	@RequestMapping("/show")
	public String show(HttpServletRequest request,Model model,String seasonId,
			PreliminaryContest preliminaryContest){
		//获取双方球队基本信息
		Map<String,Object> teamMap = preliminaryContestMapper.findTeamByContestId(preliminaryContest.getId());
		String team_one_id = teamMap.get("team_id").toString();
		String team_two_id = teamMap.get("team_id_two").toString();
		//获取球队信息
		Team team1 = teamMapper.getTeamAndSchoolById(team_one_id);
		Team team2 = teamMapper.getTeamAndSchoolById(team_two_id);	
		//获取比分
		Map<String,Object> point1 = new HashMap<String,Object>();
		Map<String,Object> point2 = new HashMap<String,Object>();
		point1.put("team_id", team_one_id);
		point1.put("contest_id", preliminaryContest.getId());
		point2.put("team_id", team_two_id);
		point2.put("contest_id", preliminaryContest.getId());
		team1.setTotalPoint(teamDateMapper.getTotalPoint(point1));
		team1.setLogUrl2(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+team1.getLogUrl2(),"ori"));
		team2.setTotalPoint(teamDateMapper.getTotalPoint(point2));
		team2.setLogUrl2(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+team2.getLogUrl2(),"ori"));
		model.addAttribute("team1", team1);
		model.addAttribute("team2", team2);		
		//球队详细数据
		point1.clear();
		point2.clear();
		point1.put("contestId", preliminaryContest.getId());
		List<TeamDate> dataList = teamDateMapper.queryTeamDateByContest(point1);
		point1.put("teamId", team_one_id);
		point2.put("teamId", team_two_id);
		point2.put("contestId", preliminaryContest.getId());
		TeamDate data1 = teamDateMapper.queryTeamDateByContest(point1).get(0);
		TeamDate data2 = teamDateMapper.queryTeamDateByContest(point2).get(0);
		model.addAttribute("dataList", dataList);
		model.addAttribute("data1", data1);
		model.addAttribute("data2", data2);
		//根据球队获取球员信息
		List<Player> playerList1 = playerMapper.queryPlayerByTeamId(point1);
		List<Player> playerList2 = playerMapper.queryPlayerByTeamId(point2);
		model.addAttribute("playerList1", playerList1);
		model.addAttribute("playerList2", playerList2);
		/*//广告
		Advert adv1=new Advert();
		Advert adv2=new Advert();
		Map<String, Object> filter = new HashMap<String, Object>();
		Page<Advert> headAdvertPage=new Page<Advert>();
		filter.put("name", "技术统计");
	    filter.put("status", 1);
	    headAdvertPage.setLimit(2);
	    headAdvertPage.setFilter(filter);
	    headAdvertPage.setResult(replace1(advertMapper.queryAdvertList(headAdvertPage)));
	    if(headAdvertPage!=null && headAdvertPage.getResult().size()>0){
	    	adv1=headAdvertPage.getResult().get(0);
	    	adv2=headAdvertPage.getResult().get(1);
	    }*/
		//原广告位修改为投篮分布点全场图
		point1.put("contest_id", preliminaryContest.getId());
		point1.put("team_id", team_one_id);
		point2.put("team_id", team_two_id);
		point2.put("contest_id", preliminaryContest.getId());
		ShootSpot shoot1 = shootSpotMapper.getshootSpotByTeamAndContest(point1);
		ShootSpot shoot2 = shootSpotMapper.getshootSpotByTeamAndContest(point2);
	    CompetitionSeason season = competitionSeasonMapper.findCompetitionSeasonById(Long.parseLong(seasonId));
	    model.addAttribute("season", season);
	    model.addAttribute("shoot1", replaceShootSpot(shoot1));
	    model.addAttribute("shoot2", replaceShootSpot(shoot2));
	  /*  model.addAttribute("adv1", adv1);
	    model.addAttribute("adv2", adv2);
	    model.addAttribute("headAdvertPage", headAdvertPage);*/
		return "foreground/data_static";
		
	}
	/**
	 * 广告图片路径替换
	 * @param imgStorageList
	 * @return
	 */
	public List<Advert> replace1(List<Advert> advertList){
		if(advertList!=null && advertList.size()>0){
			for(Advert adv:advertList){
				adv.setAdvertImg(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+adv.getAdvertImg(), "ori"));
			}
		}
		return advertList;
	}
	/**
	 * 图片路径替换
	 * @param imgStorageList
	 * @return
	 */
	public List<Advert> replace(List<Advert> advertList){
		if(advertList!=null && advertList.size()>0){
			for(Advert adv:advertList){
				adv.setAdvertImg(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+adv.getAdvertImg(), "ori"));
			}
		}
		return advertList;
	}
	public ShootSpot replaceShootSpot(ShootSpot shoot){
		shoot.setFullTimeUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+shoot.getFullTimeUrl(), "ori"));
		return shoot;
	}
}
